
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Java Lock之ReentrantLock · tgor note</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="tgor">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-page-toc-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Java之Lock.html" />
    
    
    <link rel="prev" href="Java并发.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Me
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../category/微服务.html">
            
                <a href="../category/微服务.html">
            
                    
                    微服务
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../Java-Spring/Feign.html">
            
                <a href="../Java-Spring/Feign.html">
            
                    
                    HTTP客户端Feign
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../Java-其他库/限流.html">
            
                <a href="../Java-其他库/限流.html">
            
                    
                    限流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../运维/Nginx限流.html">
            
                <a href="../运维/Nginx限流.html">
            
                    
                    限流Nginx
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../Java-Spring/负载均衡.html">
            
                <a href="../Java-Spring/负载均衡.html">
            
                    
                    负载均衡
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../运维/Nginx负载均衡.html">
            
                <a href="../运维/Nginx负载均衡.html">
            
                    
                    负载均衡Nginx
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../Java-其他库/熔断Hystrix.html">
            
                <a href="../Java-其他库/熔断Hystrix.html">
            
                    
                    熔断Hystrix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../Java-其他库/缓存.html">
            
                <a href="../Java-其他库/缓存.html">
            
                    
                    缓存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../Java-其他库/缓存.html">
            
                <a href="../Java-其他库/缓存.html#本地缓存">
            
                    
                    本地缓存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html">
            
                    
                    分布式缓存Redis
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="../Java-其他库/分布式事务.html">
            
                <a href="../Java-其他库/分布式事务.html">
            
                    
                    分布式事务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.11" data-path="../Java-其他库/分布式事务.html">
            
                <a href="../Java-其他库/分布式事务.html#事务消息">
            
                    
                    可靠事务消息
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="../Java-Spring/业务监控.html">
            
                <a href="../Java-Spring/业务监控.html">
            
                    
                    业务监控
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.13" data-path="../Java-其他库/分布式定时作业.html">
            
                <a href="../Java-其他库/分布式定时作业.html">
            
                    
                    分布式作业调度ElasticJob
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.14" data-path="../Java-ORM/ORM.html">
            
                <a href="../Java-ORM/ORM.html">
            
                    
                    ORM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.15" data-path="../Java-其他库/分布式系统及CAP原理.html">
            
                <a href="../Java-其他库/分布式系统及CAP原理.html">
            
                    
                    分布式系统及CAP原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.16" data-path="../MQ/RabbitMQ笔记.html">
            
                <a href="../MQ/RabbitMQ笔记.html">
            
                    
                    消息驱动Rabbitmq
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../category/数据库.html">
            
                <a href="../category/数据库.html">
            
                    
                    数据库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../Mysql/经典数据库设计案例.html">
            
                <a href="../Mysql/经典数据库设计案例.html">
            
                    
                    mysql设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../Mysql/Mysql事务与锁.html">
            
                <a href="../Mysql/Mysql事务与锁.html">
            
                    
                    mysql事务与锁
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../Mysql/mysql数据类型.html">
            
                <a href="../Mysql/mysql数据类型.html">
            
                    
                    mysql数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../Mysql/mysql索引及查询.html">
            
                <a href="../Mysql/mysql索引及查询.html">
            
                    
                    mysql索引与查询
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../Mysql/mysql索引及查询.html">
            
                <a href="../Mysql/mysql索引及查询.html#查询语句设计准则">
            
                    
                    mysql查询语句设计准则
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../Mysql/mysql性能测试.html">
            
                <a href="../Mysql/mysql性能测试.html">
            
                    
                    mysql性能测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../Mysql/mysql主从同步与读写分离.html">
            
                <a href="../Mysql/mysql主从同步与读写分离.html">
            
                    
                    mysql主从同步与读写分离
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../Java-Spring/SpringBoot事务注解@Transactional.html">
            
                <a href="../Java-Spring/SpringBoot事务注解@Transactional.html">
            
                    
                    Spring事务注解@Transactional
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="../数据结构与算法/位运算应用实例.html">
            
                <a href="../数据结构与算法/位运算应用实例.html">
            
                    
                    位运算应用实例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="../Mongodb-Client/morphia-API汇总.html">
            
                <a href="../Mongodb-Client/morphia-API汇总.html">
            
                    
                    morphia API汇总
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="../Mongodb-Client/mongo驱动连接.html">
            
                <a href="../Mongodb-Client/mongo驱动连接.html">
            
                    
                    mongodb驱动与连接
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="../Mongodb/mongodb-ObjectId实践.html">
            
                <a href="../Mongodb/mongodb-ObjectId实践.html">
            
                    
                    mongodb ObjectId实践
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="../Mongodb/mongodb角色权限.html">
            
                <a href="../Mongodb/mongodb角色权限.html">
            
                    
                    mongodb角色权限
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../category/java并发.html">
            
                <a href="../category/java并发.html">
            
                    
                    Java并发
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="Java并发.html">
            
                <a href="Java并发.html">
            
                    
                    Java并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="Java并发.html">
            
                <a href="Java并发.html#锁">
            
                    
                    Java并发之锁设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="Java并发.html">
            
                <a href="Java并发.html#synchronized">
            
                    
                    Java并发之synchronized
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="Java之Lock.html">
            
                <a href="Java之Lock.html#reentrantlock">
            
                    
                    Java Lock之ReentrantLock
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="Java之Lock.html">
            
                <a href="Java之Lock.html#reentrantreadwritelock">
            
                    
                    Java Lock之ReentrantReadWriteLock
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="Java之Future.html">
            
                <a href="Java之Future.html">
            
                    
                    Java Future
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="Java之Future.html">
            
                <a href="Java之Future.html#futuretask">
            
                    
                    Java Future之FutureTask
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="Java之Future.html">
            
                <a href="Java之Future.html#completablefuture">
            
                    
                    Java Future之CompletableFuture
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="Java之Future.html">
            
                <a href="Java之Future.html#forkjointask">
            
                    
                    Java Future之ForkJoinTask
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="Java之Thread.html">
            
                <a href="Java之Thread.html">
            
                    
                    Java Thread
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../category/redis.html">
            
                <a href="../category/redis.html">
            
                    
                    Redis
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html">
            
                    
                    Redis
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html#集群">
            
                    
                    Redis集群
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html#消息订阅">
            
                    
                    Redis消息订阅
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html#lua">
            
                    
                    Redis Lua脚本
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html#配置汇总">
            
                    
                    Redis配置汇总
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../category/运维.html">
            
                <a href="../category/运维.html">
            
                    
                    运维
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../运维/Linux常用操作汇总.html">
            
                <a href="../运维/Linux常用操作汇总.html">
            
                    
                    Linux常用操作汇总
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../运维/阿里云笔记.html">
            
                <a href="../运维/阿里云笔记.html">
            
                    
                    阿里云笔记
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../category/nginx.html">
            
                <a href="../category/nginx.html">
            
                    
                    Nginx
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../运维/Nginx学习笔记.html">
            
                <a href="../运维/Nginx学习笔记.html">
            
                    
                    Nginx基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../运维/Nginx负载均衡.html">
            
                <a href="../运维/Nginx负载均衡.html">
            
                    
                    Nginx负载均衡
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../运维/Nginx限流.html">
            
                <a href="../运维/Nginx限流.html">
            
                    
                    Nginx限流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../运维/Nginx学习笔记.html">
            
                <a href="../运维/Nginx学习笔记.html#六、其他">
            
                    
                    Nginx客户端辅助技巧
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../category/容器化.html">
            
                <a href="../category/容器化.html">
            
                    
                    容器化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../Docker/docker笔记.html">
            
                <a href="../Docker/docker笔记.html">
            
                    
                    Docker学习笔记
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../Docker/docker笔记.html">
            
                <a href="../Docker/docker笔记.html#基础操作">
            
                    
                    Docker基础操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../Docker/docker笔记.html">
            
                <a href="../Docker/docker笔记.html#dockerfile">
            
                    
                    DockerFile
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../category/Web.html">
            
                <a href="../category/Web.html">
            
                    
                    Web
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../运维/Http入坑Https笔记.html">
            
                <a href="../运维/Http入坑Https笔记.html">
            
                    
                    Http入坑Https笔记
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../Web/阿里云OSS.html">
            
                <a href="../Web/阿里云OSS.html">
            
                    
                    OSS
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../category/java基础.html">
            
                <a href="../category/java基础.html">
            
                    
                    Java基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="Java基础.html">
            
                <a href="Java基础.html#jvm">
            
                    
                    JVM基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="Java基础.html">
            
                <a href="Java基础.html#语法糖">
            
                    
                    Java语法糖
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="Java基础.html">
            
                <a href="Java基础.html#数据结构">
            
                    
                    Java数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="Java8函数式编程.html">
            
                <a href="Java8函数式编程.html">
            
                    
                    java8函数式编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="ArrayList源码分析.html">
            
                <a href="ArrayList源码分析.html">
            
                    
                    JDK源码分析ArrayList
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.6" data-path="HashMap源码分析.html">
            
                <a href="HashMap源码分析.html">
            
                    
                    JDK源码分析HashMap
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.7" data-path="Java注解.html">
            
                <a href="Java注解.html">
            
                    
                    Java注解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.8" data-path="Timsort排序算法.html">
            
                <a href="Timsort排序算法.html">
            
                    
                    Timsort排序算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.9" data-path="插入排序分析与Java实现.html">
            
                <a href="插入排序分析与Java实现.html">
            
                    
                    插入排序分析与Java实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../category/工具与规范.html">
            
                <a href="../category/工具与规范.html">
            
                    
                    工具与规范
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../工具/Maven使用技巧汇总.html">
            
                <a href="../工具/Maven使用技巧汇总.html">
            
                    
                    Maven使用技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../编码规范/Java编码规范.html">
            
                <a href="../编码规范/Java编码规范.html">
            
                    
                    Java编码规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../category/OLAP.html">
            
                <a href="../category/OLAP.html">
            
                    
                    OLAP
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.12.1" data-path="../数据处理/storm学习笔记.html">
            
                <a href="../数据处理/storm学习笔记.html">
            
                    
                    流式计算Storm学习笔记
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../category/数据运营.html">
            
                <a href="../category/数据运营.html">
            
                    
                    数据运营
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="../数据运营/增长黑客数据运营.html">
            
                <a href="../数据运营/增长黑客数据运营.html">
            
                    
                    增长黑客-数据运营
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../category/前端技术栈.html">
            
                <a href="../category/前端技术栈.html">
            
                    
                    前端技术栈
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../前端/JavaScript设计模式.html">
            
                <a href="../前端/JavaScript设计模式.html">
            
                    
                    JavaScript设计模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../前端/html相对路径与绝对路径.html">
            
                <a href="../前端/html相对路径与绝对路径.html">
            
                    
                    Html相对路径与绝对路径
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.3" data-path="../前端/微信小程序学习笔记.html">
            
                <a href="../前端/微信小程序学习笔记.html">
            
                    
                    微信小程序学习笔记
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../category/个人.html">
            
                <a href="../category/个人.html">
            
                    
                    个人
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.15.1" data-path="../个人/简历.html">
            
                <a href="../个人/简历.html">
            
                    
                    简历
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Java Lock之ReentrantLock</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="java-lock">Java Lock</h1>
<h1 id="abstractqueuedsynchronizer">AbstractQueuedSynchronizer</h1>
<p>AQS&#x5168;&#x79F0;AbstractQueuedSynchronizer &#xFF0C;&#x62BD;&#x8C61;&#x961F;&#x5217;&#x540C;&#x6B65;&#x5668;</p>
<p>&#x5728;java.util.concurrent.locks&#x5305;&#x4E0B;&#xFF0C;&#x662F;<code>ReentrantLock</code>&#x5B9E;&#x73B0;&#x9501;&#x673A;&#x5236;&#x7684;&#x57FA;&#x7840;</p>
<h2 id="node&#x7ED3;&#x6784;&#x793A;&#x610F;">Node&#x7ED3;&#x6784;&#x793A;&#x610F;</h2>
<p><img src="assets/1580445198954.png" alt="1580445198954"></p>
<p>&#x91CD;&#x70B9;&#xFF1A;head&#x8282;&#x70B9;&#x662F;&#x961F;&#x5217;&#x521D;&#x59CB;&#x5316;&#x7684;&#x65F6;&#x5019;&#x4E00;&#x4E2A;&#x8282;&#x70B9;&#xFF0C;<strong>&#x53EA;&#x8868;&#x793A;&#x4F4D;&#x7F6E;&#xFF0C;&#x4E0D;&#x4EE3;&#x8868;&#x5B9E;&#x9645;&#x7684;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;</strong>&#x3002;head&#x8282;&#x70B9;&#x4E4B;&#x540E;&#x7684;&#x8282;&#x70B9;&#x5C31;&#x662F;&#x83B7;&#x53D6;&#x9501;&#x5931;&#x8D25;&#x8FDB;&#x5165;&#x7B49;&#x5F85;&#x961F;&#x5217;&#x7684;&#x7EBF;&#x7A0B;&#x3002;</p>
<h1 id="reentrantlock">ReentrantLock</h1>
<h2 id="&#x57FA;&#x7840;">&#x57FA;&#x7840;</h2>
<p>&#x4E0E;<code>synchronized</code>&#x6BD4;&#x8F83;&#xFF0C;&#x76F8;&#x540C;&#x70B9;&#xFF1A;</p>
<ul>
<li>&#x72EC;&#x5360;&#x9501;</li>
<li>&#x53EF;&#x91CD;&#x5165;</li>
</ul>
<p>&#x4E0D;&#x540C;&#x70B9;&#xFF1A;</p>
<ul>
<li>ReentrantLock&#x663E;&#x5F0F;&#x4E0A;&#x9501;&#xFF0C;&#x8981;&#x6C42;&#x624B;&#x52A8;&#x4E0A;&#x9501;&#x53CA;&#x91CA;&#x653E;</li>
<li>&#x66F4;&#x7075;&#x6D3B;&#x3001;&#x66F4;&#x591A;&#x6269;&#x5C55;&#x529F;&#x80FD;</li>
</ul>
<p>&#x4E0E;<code>ReentrantReadWriteLock</code>&#x6BD4;&#x8F83;&#xFF0C;&#x76F8;&#x540C;&#x70B9;</p>
<ul>
<li>&#x90FD;&#x662F;Lock&#x90FD;&#x4E00;&#x79CD;&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;</li>
<li>&#x652F;&#x6301;&#x516C;&#x5E73;&#x9501;&#x4E0E;&#x975E;&#x516C;&#x5E73;&#x9501;</li>
</ul>
<p>&#x4E0D;&#x540C;&#x70B9;&#xFF1A;</p>
<ul>
<li><code>ReentrantLock</code>&#x662F;&#x4E00;&#x4E2A;&#x6392;&#x4ED6;&#x9501;&#xFF0C;&#x540C;&#x65F6;&#x95F4;&#x53EA;&#x5141;&#x8BB8;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x8BBF;&#x95EE;</li>
<li><code>ReentrantReadWriteLock</code>&#x5141;&#x8BB8;&#x591A;&#x4E2A;&#x8BFB;&#x7EBF;&#x7A0B;&#x8BBF;&#x95EE;</li>
</ul>
<p>&#x5165;&#x95E8;&#x793A;&#x4F8B;</p>
<pre><code class="lang-java">ReentrantLock lock = <span class="hljs-keyword">new</span> ReentrantLock();

<span class="hljs-comment">//&#x4E0A;&#x9501;&#xFF0C;&#x7F3A;&#x9501;&#x65F6;&#xFF0C;&#x963B;&#x585E;&#x76F4;&#x81F3;&#x83B7;&#x5F97;&#x9501;</span>
lock.lock();

<span class="hljs-comment">//&#x4E0A;&#x9501;&#xFF0C;&#x7F3A;&#x9501;&#x65F6;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;false</span>
lock.tryLock();

<span class="hljs-comment">//&#x4E0A;&#x9501;&#xFF0C;&#x8D85;&#x65F6;&#x4ECD;&#x7F3A;&#x65F6;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;false</span>
lock.tryLock(<span class="hljs-keyword">long</span> timeout, TimeUnit unit);

<span class="hljs-comment">//&#x64CD;&#x4F5C;&#x5171;&#x4EAB;&#x5BF9;&#x8C61;</span>

<span class="hljs-comment">//&#x91CA;&#x653E;&#x9501;</span>
lock.unlock();
</code></pre>
<h3 id="&#x963B;&#x585E;&#x4E0E;&#x975E;&#x963B;&#x585E;">&#x963B;&#x585E;&#x4E0E;&#x975E;&#x963B;&#x585E;</h3>
<pre><code class="lang-java"><span class="hljs-comment">/**
 * &#x6BCF;&#x6761;&#x7EBF;&#x7A0B;&#x52A0;&#x9501;&#x540E;&#x6302;&#x8D77;200ms
 * &#x7EBF;&#x7A0B;&#x90FD;&#x963B;&#x585E;&#x7B49;&#x5F85;&#xFF0C;&#x76F4;&#x81F3;&#x83B7;&#x5F97;&#x9501;
 */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">lockExample</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">5</span>; i++) {
        <span class="hljs-keyword">new</span> Thread(() -&gt; {
            System.out.println(Thread.currentThread().getName() + <span class="hljs-string">&quot;&#x5C1D;&#x8BD5;&#x83B7;&#x5F97;&#x9501;&quot;</span>);
            lock.lock();
            System.out.println(Thread.currentThread().getName() + <span class="hljs-string">&quot;&#x6210;&#x529F;&quot;</span>);
            threadSleep(<span class="hljs-number">200</span>);

            lock.unlock();
            System.out.println(Thread.currentThread().getName() + <span class="hljs-string">&quot;&#x91CA;&#x653E;&quot;</span>);
        }).start();
    }
}

<span class="hljs-comment">/**
 * &#x6BCF;&#x6761;&#x7EBF;&#x7A0B;&#x52A0;&#x9501;&#x540E;&#x6302;&#x8D77;200ms
 * &#x9664;&#x7B2C;&#x4E00;&#x6761;&#x7EBF;&#x7A0B;&#x4EE5;&#x5916;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x5747;&#x5931;&#x8D25;
 */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">tryLockExample</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">5</span>; i++) {
        <span class="hljs-keyword">new</span> Thread(() -&gt; {

            System.out.println(Thread.currentThread().getName() + <span class="hljs-string">&quot;&#x5C1D;&#x8BD5;&#x83B7;&#x5F97;&#x9501;&quot;</span>);
            <span class="hljs-keyword">boolean</span> ok = lock.tryLock();
            <span class="hljs-keyword">if</span> (ok) {
                System.out.println(Thread.currentThread().getName() + <span class="hljs-string">&quot;&#x6210;&#x529F;&quot;</span>);
                threadSleep(<span class="hljs-number">200</span>);
                lock.unlock();
                System.out.println(Thread.currentThread().getName() + <span class="hljs-string">&quot;&#x91CA;&#x653E;&quot;</span>);
            } <span class="hljs-keyword">else</span> {
                System.out.println(Thread.currentThread().getName() + <span class="hljs-string">&quot;&#x5931;&#x8D25;&quot;</span>);
            }

        }).start();
    }
}

<span class="hljs-comment">/**
 * tryLock&#x963B;&#x585E;&#x8D85;&#x65F6;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;false&#xFF0C;&#x672A;&#x8D85;&#x65F6;&#x5219;&#x963B;&#x585E;&#x7B49;&#x5F85;
 */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">tryLockExampleTimeOut</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">5</span>; i++) {
        <span class="hljs-keyword">new</span> Thread(() -&gt; {
            System.out.println(Thread.currentThread().getName() + <span class="hljs-string">&quot;&#x5C1D;&#x8BD5;&#x83B7;&#x5F97;&#x9501;&quot;</span>);
            <span class="hljs-keyword">boolean</span> ok = <span class="hljs-keyword">false</span>;
            <span class="hljs-keyword">try</span> {
                ok = lock.tryLock(<span class="hljs-number">200L</span>, TimeUnit.MILLISECONDS);
            } <span class="hljs-keyword">catch</span> (InterruptedException ex) {
                ex.printStackTrace();
            }
            <span class="hljs-keyword">if</span> (ok) {
                System.out.println(Thread.currentThread().getName() + <span class="hljs-string">&quot;&#x6210;&#x529F;&quot;</span>);
                <span class="hljs-comment">//</span>
                threadSleep(<span class="hljs-number">300</span>);
                lock.unlock();
                System.out.println(Thread.currentThread().getName() + <span class="hljs-string">&quot;&#x91CA;&#x653E;&quot;</span>);
            } <span class="hljs-keyword">else</span> {
                System.out.println(Thread.currentThread().getName() + <span class="hljs-string">&quot;&#x5931;&#x8D25;&quot;</span>);
            }

        }).start();
    }
}
</code></pre>
<h3 id="&#x516C;&#x5E73;&#x9501;&#x4E0E;&#x975E;&#x516C;&#x5E73;&#x9501;">&#x516C;&#x5E73;&#x9501;&#x4E0E;&#x975E;&#x516C;&#x5E73;&#x9501;</h3>
<p><img src="assets/1580272552797.png" alt="1580272552797"></p>
<p>&#x4ECE;&#x539F;&#x7406;&#x89D2;&#x5EA6;&#x5206;&#x6790;&#xFF1A;</p>
<p><strong>&#x516C;&#x5E73;&#x9501;&#x6BCF;&#x6B21;&#x90FD;&#x662F;&#x4ECE;&#x540C;&#x6B65;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x8282;&#x70B9;&#x83B7;&#x53D6;&#x5230;&#x9501;&#xFF0C;&#x800C;&#x975E;&#x516C;&#x5E73;&#x6027;&#x9501;&#x5219;&#x4E0D;&#x4E00;&#x5B9A;&#xFF0C;&#x6709;&#x53EF;&#x80FD;&#x521A;&#x91CA;&#x653E;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#x80FD;&#x518D;&#x6B21;&#x83B7;&#x53D6;&#x5230;&#x9501;</strong>&#x3002;</p>
<ul>
<li>&#x516C;&#x5E73;&#x9501;&#xFF1A;&#x907F;&#x514D;&#x7EBF;&#x7A0B;&#x9965;&#x997F;&#x95EE;&#x9898;</li>
<li>&#x975E;&#x516C;&#x5E73;&#x9501;&#xFF1A;&#x6027;&#x80FD;&#x66F4;&#x597D;&#xFF0C;&#x662F;&#x9996;&#x9009;&#x9879;&#xFF0C;<strong>&#x4E0E;synchronized&#x4E00;&#x81F4;</strong></li>
</ul>
<p>&#x4F7F;&#x7528;&#x65B9;&#x6CD5;&#xFF1A;ReentrantLock&#x6784;&#x9020;&#x51FD;&#x6570;&#x51B3;&#x5B9A;</p>
<pre><code class="lang-java"><span class="hljs-comment">//true&#x516C;&#x5E73;&#x9501;&#xFF0C;&#x9ED8;&#x8BA4;false</span>
ReentrantLock lock = <span class="hljs-keyword">new</span> ReentrantLock(<span class="hljs-keyword">true</span>);
</code></pre>
<p>ReentrantLock&#x6784;&#x9020;&#x51FD;&#x6570;&#x6E90;&#x7801;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ReentrantLock</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> fair)</span> </span>{
    sync = fair ? <span class="hljs-keyword">new</span> FairSync() : <span class="hljs-keyword">new</span> NonfairSync();
}
</code></pre>
<p>&#x793A;&#x4F8B;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ReentrantLockTest</span> </span>{

    <span class="hljs-comment">//&#x9ED8;&#x8BA4;&#x975E;&#x516C;&#x5E73;&#xFF0C;false</span>
    <span class="hljs-keyword">static</span> Lock lock = <span class="hljs-keyword">new</span> ReentrantLock(<span class="hljs-keyword">true</span>);

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> InterruptedException </span>{

        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">5</span>; i++) {
            <span class="hljs-keyword">new</span> Thread(<span class="hljs-keyword">new</span> ThreadDemo(i)).start();
        }

    }

    <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ThreadDemo</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Runnable</span> </span>{
        Integer id;

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ThreadDemo</span><span class="hljs-params">(Integer id)</span> </span>{
            <span class="hljs-keyword">this</span>.id = id;
        }

        <span class="hljs-meta">@Override</span>

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
            <span class="hljs-keyword">try</span> {
                TimeUnit.MILLISECONDS.sleep(<span class="hljs-number">10</span>);
            } <span class="hljs-keyword">catch</span> (InterruptedException e) {
                e.printStackTrace();
            }
            <span class="hljs-comment">//&#x83B7;&#x5F97;&#x9501;&#xFF0C;&#x91CA;&#x653E;&#xFF0C;&#x518D;&#x6B21;&#x83B7;&#x5F97;&#x9501;&#xFF0C;&#x518D;&#x6B21;&#x91CA;&#x653E;</span>
            <span class="hljs-comment">//&#x975E;&#x516C;&#x5E73;&#xFF1A;&#x540C;&#x4E00;&#x7EBF;&#x7A0B;&#x518D;&#x6B21;&#x83B7;&#x5F97;&#x9501;</span>
            <span class="hljs-comment">//&#x516C;&#x5E73;&#xFF1A;&#x8BE5;&#x7EBF;&#x7A0B;&#x518D;&#x53BB;&#x6392;&#x961F;</span>
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">2</span>; i++) {
                lock.lock();
                System.out.println(<span class="hljs-string">&quot;&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;&quot;</span> + id);
                lock.unlock();
            }
        }
    }
}


<span class="hljs-comment">//&#x516C;&#x5E73;&#x9501;&#x7ED3;&#x679C;</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">2</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">1</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">4</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">0</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">3</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">2</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">1</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">4</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">0</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">3</span>


<span class="hljs-comment">//&#x975E;&#x516C;&#x5E73;&#x9501;&#x7ED3;&#x679C;</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">2</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">2</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">0</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">0</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">3</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">3</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">1</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">1</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">4</span>
&#x83B7;&#x5F97;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;<span class="hljs-number">4</span>
</code></pre>
<p>&#x770B;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF0C;&#x975E;&#x516C;&#x5E73;&#x9501;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x540C;&#x4E00;&#x7EBF;&#x7A0B;&#x518D;&#x6B21;&#x83B7;&#x5F97;&#x9501;&#x65F6;&#xFF0C;&#x4F9D;&#x7136;&#x4E3A;&#x4E0A;&#x4E00;&#x6B21;&#x83B7;&#x5F97;&#x7684;&#x9501;</p>
<h3 id="&#x54CD;&#x5E94;&#x4E2D;&#x65AD;">&#x54CD;&#x5E94;&#x4E2D;&#x65AD;</h3>
<p>&#x901A;&#x8FC7;ReentrantLock&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x6B7B;&#x9501;&#x7684;&#x4F8B;&#x5B50;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ReentrantLockInterruptTest</span> </span>{
    <span class="hljs-keyword">static</span> Lock lock1 = <span class="hljs-keyword">new</span> ReentrantLock();
    <span class="hljs-keyword">static</span> Lock lock2 = <span class="hljs-keyword">new</span> ReentrantLock();
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> InterruptedException </span>{

        Thread thread = <span class="hljs-keyword">new</span> Thread(<span class="hljs-keyword">new</span> ThreadDemo(lock1, lock2));<span class="hljs-comment">//&#x8BE5;&#x7EBF;&#x7A0B;&#x5148;&#x83B7;&#x53D6;&#x9501;1,&#x518D;&#x83B7;&#x53D6;&#x9501;2</span>
        Thread thread1 = <span class="hljs-keyword">new</span> Thread(<span class="hljs-keyword">new</span> ThreadDemo(lock2, lock1));<span class="hljs-comment">//&#x8BE5;&#x7EBF;&#x7A0B;&#x5148;&#x83B7;&#x53D6;&#x9501;2,&#x518D;&#x83B7;&#x53D6;&#x9501;1</span>
        thread.start();
        thread1.start();
<span class="hljs-comment">//        thread.interrupt();//&#x662F;&#x7B2C;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E2D;&#x65AD;&#xFF0C;&#x4E2D;&#x65AD;&#x540E;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x7EBF;&#x7A0B;&#x53EF;&#x4EE5;&#x6B63;&#x5E38;&#x6267;&#x884C;</span>
    }

    <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ThreadDemo</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Runnable</span> </span>{
        Lock firstLock;
        Lock secondLock;
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ThreadDemo</span><span class="hljs-params">(Lock firstLock, Lock secondLock)</span> </span>{
            <span class="hljs-keyword">this</span>.firstLock = firstLock;
            <span class="hljs-keyword">this</span>.secondLock = secondLock;
        }
        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
            <span class="hljs-keyword">try</span> {
                firstLock.lock();
                TimeUnit.MILLISECONDS.sleep(<span class="hljs-number">10</span>);<span class="hljs-comment">//&#x66F4;&#x597D;&#x7684;&#x89E6;&#x53D1;&#x6B7B;&#x9501;</span>
                secondLock.lock();
            } <span class="hljs-keyword">catch</span> (InterruptedException e) {
                e.printStackTrace();
            } <span class="hljs-keyword">finally</span> {
                firstLock.unlock();
                secondLock.unlock();
                System.out.println(Thread.currentThread().getName()+<span class="hljs-string">&quot;&#x6B63;&#x5E38;&#x7ED3;&#x675F;!&quot;</span>);
            }
        }
    }
}

<span class="hljs-comment">//output</span>
<span class="hljs-comment">//&#x8FDB;&#x7A0B;&#x6C38;&#x8FDC;&#x4E0D;&#x4F1A;&#x7ED3;&#x675F;&#xFF0C;&#x89E6;&#x53D1;&#x6B7B;&#x9501;</span>
</code></pre>
<p>&#x54CD;&#x5E94;&#x4E2D;&#x65AD;&#x53EF;&#x4EE5;&#x89E3;&#x51B3;&#x6B7B;&#x9501;&#x95EE;&#x9898;</p>
<p>&#x901A;&#x8FC7;<code>lockInterruptibly()</code>&#x5B9E;&#x73B0;</p>
<h3 id="&#x8D85;&#x65F6;&#x7B49;&#x5F85;">&#x8D85;&#x65F6;&#x7B49;&#x5F85;</h3>
<p>TODO</p>
<h3 id="&#x914D;&#x5408;condition&#x5B9E;&#x73B0;&#x7B49;&#x5F85;&#x901A;&#x77E5;&#x673A;&#x5236;">&#x914D;&#x5408;Condition&#x5B9E;&#x73B0;&#x7B49;&#x5F85;&#x901A;&#x77E5;&#x673A;&#x5236;</h3>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ConditionTest</span> </span>{
    <span class="hljs-keyword">static</span> ReentrantLock lock = <span class="hljs-keyword">new</span> ReentrantLock();
    <span class="hljs-keyword">static</span> Condition condition = lock.newCondition();

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> InterruptedException </span>{

        lock.lock();
        <span class="hljs-keyword">new</span> Thread(<span class="hljs-keyword">new</span> SignalThread(lock, condition)).start();
        System.out.println(<span class="hljs-string">&quot;&#x4E3B;&#x7EBF;&#x7A0B;&#x7B49;&#x5F85;&#x901A;&#x77E5;&quot;</span>);
        <span class="hljs-keyword">try</span> {
            <span class="hljs-comment">//&#x963B;&#x585E;&#x7B49;&#x5F85;</span>
            condition.await();
        } <span class="hljs-keyword">finally</span> {
            lock.unlock();
        }
        System.out.println(<span class="hljs-string">&quot;&#x4E3B;&#x7EBF;&#x7A0B;&#x6062;&#x590D;&#x8FD0;&#x884C;&quot;</span>);
    }

    <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SignalThread</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Runnable</span> </span>{

        <span class="hljs-keyword">private</span> Lock mylock;

        <span class="hljs-keyword">private</span> Condition myCondition;

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">SignalThread</span><span class="hljs-params">(Lock mylock, Condition myCondition)</span> </span>{
            <span class="hljs-keyword">this</span>.mylock = mylock;
            <span class="hljs-keyword">this</span>.myCondition = myCondition;
        }

        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
            mylock.lock();
            <span class="hljs-keyword">try</span> {
                <span class="hljs-comment">//signal&#x901A;&#x77E5;</span>
                myCondition.signal();
                System.out.println(<span class="hljs-string">&quot;&#x5B50;&#x7EBF;&#x7A0B;&#x901A;&#x77E5;&quot;</span>);
            } <span class="hljs-keyword">finally</span> {
                mylock.unlock();
            }
        }
    }
}
</code></pre>
<p><img src="assets/1580352262934.png" alt="1580352262934"></p>
<p>ReentrantLock&#x5185;&#x7F6E;&#x4E24;&#x4E2A;&#x540C;&#x6B65;&#x5668;&#xFF0C;&#x5206;&#x522B;&#x516C;&#x5E73;&#x9501;&#x7684;<code>FairSync</code>&#x4E0E;&#x975E;&#x516C;&#x5E73;&#x9501;&#x7684;<code>NonfairSync</code>&#xFF0C;&#x5747;&#x5B9E;&#x73B0;AQS</p>
<h2 id="&#x4E0A;&#x9501;&#x5206;&#x6790;">&#x4E0A;&#x9501;&#x5206;&#x6790;</h2>
<p>lock&#x6D41;&#x7A0B;&#x56FE;</p>
<p><img src="assets/aqs.jpg" alt="1580352262935"></p>
<h3 id="&#x540C;&#x6B65;&#x5668;sync&#x4EE3;&#x7801;">&#x540C;&#x6B65;&#x5668;Sync&#x4EE3;&#x7801;</h3>
<pre><code class="lang-java"><span class="hljs-comment">//&#x975E;&#x516C;&#x5E73;&#x9501;&#x540C;&#x6B65;&#x5668;</span>
<span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NonfairSync</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Sync</span> </span>{
  <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> serialVersionUID = <span class="hljs-number">7316153563782823691L</span>;
     <span class="hljs-comment">/**
     * Performs lock.  Try immediate barge, backing up to normal
     * acquire on failure.
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">final</span> <span class="hljs-keyword">void</span> <span class="hljs-title">lock</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">if</span> (compareAndSetState(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>))
            setExclusiveOwnerThread(Thread.currentThread());
        <span class="hljs-keyword">else</span>
            <span class="hljs-comment">/**
             * &#x6838;&#x5FC3;AQS&#x83B7;&#x53D6;&#x9501;&#x65B9;&#x6CD5;
             **/</span>
            acquire(<span class="hljs-number">1</span>);
    }

    <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">tryAcquire</span><span class="hljs-params">(<span class="hljs-keyword">int</span> acquires)</span> </span>{
        <span class="hljs-keyword">return</span> nonfairTryAcquire(acquires);
    }
}

<span class="hljs-comment">//&#x516C;&#x5E73;&#x9501;&#x540C;&#x6B65;&#x5668;</span>
<span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FairSync</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Sync</span> </span>{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> serialVersionUID = -<span class="hljs-number">3000897897090466540L</span>;

    <span class="hljs-function"><span class="hljs-keyword">final</span> <span class="hljs-keyword">void</span> <span class="hljs-title">lock</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-comment">/**
        * &#x6838;&#x5FC3;AQS&#x83B7;&#x53D6;&#x9501;&#x65B9;&#x6CD5;
        **/</span>
        acquire(<span class="hljs-number">1</span>);
    }

    <span class="hljs-comment">/**
     * Fair version of tryAcquire.  Don&apos;t grant access unless
     * recursive call or no waiters or is first.
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">tryAcquire</span><span class="hljs-params">(<span class="hljs-keyword">int</span> acquires)</span> </span>{
        <span class="hljs-keyword">final</span> Thread current = Thread.currentThread();
        <span class="hljs-keyword">int</span> c = getState();
        <span class="hljs-keyword">if</span> (c == <span class="hljs-number">0</span>) {
            <span class="hljs-keyword">if</span> (!hasQueuedPredecessors() &amp;&amp;
                compareAndSetState(<span class="hljs-number">0</span>, acquires)) {
                setExclusiveOwnerThread(current);
                <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
            }
        }
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (current == getExclusiveOwnerThread()) {
            <span class="hljs-keyword">int</span> nextc = c + acquires;
            <span class="hljs-keyword">if</span> (nextc &lt; <span class="hljs-number">0</span>)
                <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Error(<span class="hljs-string">&quot;Maximum lock count exceeded&quot;</span>);
            setState(nextc);
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
        }
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
    }
}
</code></pre>
<h3 id="aqs&#x4E4B;acquire&#x4EE3;&#x7801;">AQS&#x4E4B;<code>acquire()</code>&#x4EE3;&#x7801;</h3>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AbstractQueuedSynchronizer</span></span>{
    <span class="hljs-comment">/**
     * &#x83B7;&#x53D6;&#x9501;&#x65B9;&#x6CD5;
     **/</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">void</span> <span class="hljs-title">acquire</span><span class="hljs-params">(<span class="hljs-keyword">int</span> arg)</span> </span>{
        <span class="hljs-keyword">if</span> (!tryAcquire(arg) &amp;&amp; acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
            selfInterrupt();
    }

    <span class="hljs-comment">/**
     * &#x4E3A;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x751F;&#x6210;node&#x8282;&#x70B9;
     * &#x63A5;&#x5165;tail&#x5C3E;&#x90E8;&#x8282;&#x70B9;
     * &#x751F;&#x6210;&#x6700;&#x65B0;&#x94FE;&#x8868;
     **/</span>
    <span class="hljs-function"><span class="hljs-keyword">private</span> Node <span class="hljs-title">addWaiter</span><span class="hljs-params">(Node mode)</span> </span>{
        Node node = <span class="hljs-keyword">new</span> Node(Thread.currentThread(), mode);
        Node pred = tail;
        <span class="hljs-keyword">if</span> (pred != <span class="hljs-keyword">null</span>) {
            node.prev = pred;
            <span class="hljs-comment">//&#x901A;&#x8FC7;cas&#x66F4;&#x6362;&#x5C3E;&#x90E8;node</span>
            <span class="hljs-keyword">if</span> (compareAndSetTail(pred, node)) {
                pred.next = node;
                <span class="hljs-keyword">return</span> node;
            }
        }
        enq(node);
        <span class="hljs-keyword">return</span> node;
    }

    <span class="hljs-comment">/**
     * &#x65E0;&#x9650;&#x5FAA;&#x73AF;&#xFF0C;&#x76F4;&#x81F3;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x6D6E;&#x51FA;&#x961F;&#x5217;&#x5934;&#x90E8;&#x5E76;tryAcquire&#x83B7;&#x5F97;&#x9501;
     * &#x5E76;&#x8FD4;&#x56DE;&#x7EBF;&#x7A0B;&#x662F;&#x5426;&#x4E2D;&#x65AD;&#x7ED3;&#x679C;
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">acquireQueued</span><span class="hljs-params">(<span class="hljs-keyword">final</span> Node node, <span class="hljs-keyword">int</span> arg)</span> </span>{
        <span class="hljs-keyword">boolean</span> failed = <span class="hljs-keyword">true</span>;
        <span class="hljs-keyword">try</span> {
            <span class="hljs-keyword">boolean</span> interrupted = <span class="hljs-keyword">false</span>;
            <span class="hljs-keyword">for</span> (;;) {
                <span class="hljs-comment">//&#x83B7;&#x53D6;&#x5F53;&#x524D;node&#x7684;prev&#x8282;&#x70B9;</span>
                <span class="hljs-keyword">final</span> Node p = node.predecessor();
                <span class="hljs-comment">//&#x5982;&#x679C;prev&#x4E3A;head&#xFF0C;&#x4E14;&#x5B50;&#x7C7B;&#x5C1D;&#x8BD5;&#x83B7;&#x53D6;&#x9501;&#x6210;&#x529F;&#xFF0C;&#x5373;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x5728;&#x94FE;&#x8868;&#x5934;</span>
                <span class="hljs-comment">//&#x5219;head&#x91CD;&#x7F6E;&#x3001;&#x8DF3;&#x51FA;&#x65E0;&#x9650;&#x5FAA;&#x73AF;&#xFF0C;&#x8FD4;&#x56DE;&#x83B7;&#x53D6;&#x9501;&#x6210;&#x529F;</span>
                <span class="hljs-keyword">if</span> (p == head &amp;&amp; tryAcquire(arg)) {
                    setHead(node);
                    p.next = <span class="hljs-keyword">null</span>; <span class="hljs-comment">// help GC</span>
                    failed = <span class="hljs-keyword">false</span>;
                    <span class="hljs-keyword">return</span> interrupted;
                }
                <span class="hljs-comment">//&#x5426;&#x5219;&#xFF0C;&#x6302;&#x8D77;&#x7EBF;&#x7A0B;&#x7B49;&#x5F85;</span>
                <span class="hljs-keyword">if</span> (shouldParkAfterFailedAcquire(p, node) &amp;&amp;
                    <span class="hljs-comment">//&#x7EBF;&#x7A0B;&#x6302;&#x8D77;&#x5E76;&#x68C0;&#x67E5;&#x662F;&#x5426;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x662F;&#x5426;&#x4E2D;&#x65AD;</span>
                    parkAndCheckInterrupt())
                    <span class="hljs-comment">//</span>
                    interrupted = <span class="hljs-keyword">true</span>;
            }
        } <span class="hljs-keyword">finally</span> {
            <span class="hljs-keyword">if</span> (failed)
                cancelAcquire(node);
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">parkAndCheckInterrupt</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-comment">//&#x7EBF;&#x7A0B;&#x6302;&#x8D77;&#x963B;&#x585E;</span>
        LockSupport.park(<span class="hljs-keyword">this</span>);
        <span class="hljs-keyword">return</span> Thread.interrupted();
    }
}
</code></pre>
<h2 id="&#x91CA;&#x653E;&#x9501;&#x5206;&#x6790;">&#x91CA;&#x653E;&#x9501;&#x5206;&#x6790;</h2>
<p><img src="assets/1580453597694.png" alt="1580453597694"></p>
<p>&#x91CA;&#x653E;&#x9501;&#x4E0D;&#x5B58;&#x5728;&#x516C;&#x5E73;&#x4E0E;&#x975E;&#x516C;&#x5E73;&#x7684;&#x8BF4;&#x6CD5;&#xFF0C;&#x6240;&#x4EE5;<code>FairSync</code>&#x4E0E;<code>NonfairSync</code>&#x540C;&#x6B65;&#x5668;&#x90FD;&#x6CA1;&#x6709;&#x5B9E;&#x73B0;&#x5404;&#x81EA;&#x7684;&#x91CA;&#x653E;&#x64CD;&#x4F5C;&#xFF0C;&#x91C7;&#x7528;&#x62BD;&#x8C61;&#x7C7B;<code>ReentrantLock.Sync</code>&#x7684;<code>tryRelease()</code>&#x65B9;&#x6CD5;</p>
<pre><code class="lang-java">ReentrantLock lock;
lock.unLock();

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ReentrantLock</span>()</span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">unlock</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-comment">//&#x6267;&#x884C;aqs&#x7684;release</span>
        sync.release(<span class="hljs-number">1</span>);
    }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AbstractQueuedSynchronizer</span></span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">release</span><span class="hljs-params">(<span class="hljs-keyword">int</span> arg)</span> </span>{
        <span class="hljs-comment">//tryRelease&#x4E3A;&#x62BD;&#x8C61;&#x65B9;&#x6CD5;&#xFF0C;&#x7531;ReentrantLock.Sync.tryRelease&#x5B9E;&#x73B0;</span>
        <span class="hljs-keyword">if</span> (tryRelease(arg)) {
            Node h = head;
            <span class="hljs-keyword">if</span> (h != <span class="hljs-keyword">null</span> &amp;&amp; h.waitStatus != <span class="hljs-number">0</span>)
                unparkSuccessor(h);
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
        }
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
    }

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">unparkSuccessor</span><span class="hljs-params">(Node node)</span> </span>{
        ...            
        <span class="hljs-comment">//&#x91CA;&#x653E;&#x4E0B;&#x4E00;&#x4F4D;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x7684;&#x6302;&#x8D77;&#x72B6;&#x6001;</span>
        Node s = node.next;
        <span class="hljs-keyword">if</span> (s != <span class="hljs-keyword">null</span>)
            LockSupport.unpark(s.thread);
    }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Sync</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AbstractQueuedSynchronizer</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">tryRelease</span><span class="hljs-params">(<span class="hljs-keyword">int</span> releases)</span> </span>{
        <span class="hljs-keyword">int</span> c = getState() - releases;
        <span class="hljs-comment">//&#x5F53;&#x524D;&#x9501;&#x7684;&#x6301;&#x6709;&#x7EBF;&#x7A0B;&#x4E0E;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x4E0D;&#x4E00;&#x81F4;&#x65F6;&#xFF0C;&#x62A5;&#x9519;IllegalMonitorStateException</span>
        <span class="hljs-keyword">if</span> (Thread.currentThread() != getExclusiveOwnerThread())
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalMonitorStateException();
        <span class="hljs-keyword">boolean</span> free = <span class="hljs-keyword">false</span>;
        <span class="hljs-comment">//&#x5F53;state&#x4E3A;0&#xFF0C;&#x6CA1;&#x6709;&#x7EBF;&#x7A0B;&#x7D22;&#x53D6;&#x9501;</span>
        <span class="hljs-keyword">if</span> (c == <span class="hljs-number">0</span>) {
            free = <span class="hljs-keyword">true</span>;
            setExclusiveOwnerThread(<span class="hljs-keyword">null</span>);
        }
        setState(c);
        <span class="hljs-keyword">return</span> free;
    }
}
</code></pre>
<h1 id="reentrantreadwritelock">ReentrantReadWriteLock</h1>
<h2 id="&#x57FA;&#x7840;">&#x57FA;&#x7840;</h2>
<p>&#x4E0E;<code>ReentrantLock</code>&#x6BD4;&#x8F83;&#x53C2;&#x7167;&#x4EE5;&#x4E0A;</p>
<p>&#x8BFB;&#x64CD;&#x4F5C;&#x5171;&#x4EAB;&#x9501;</p>
<p>&#x5199;&#x64CD;&#x4F5C;&#x6392;&#x4ED6;&#x9501;</p>
<p>&#x53C2;&#x8003;&#x793A;&#x4F8B;&#xFF0C;Account&#x8D26;&#x6237;&#x5BF9;&#x8C61;&#xFF0C;&#x5199;&#x9501;&#x4FDD;&#x62A4;update&#x64CD;&#x4F5C;&#xFF0C;&#x8BFB;&#x9501;&#x5141;&#x8BB8;&#x5171;&#x4EAB;&#x8BFB;&#x53D6;&#x64CD;&#x4F5C;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Account</span> </span>{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> ReentrantReadWriteLock rwl = <span class="hljs-keyword">new</span> ReentrantReadWriteLock();
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> Lock readLock = rwl.readLock();    <span class="hljs-comment">//&#x8BFB;&#x9501;</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> Lock writeLock = rwl.writeLock();    <span class="hljs-comment">//&#x5199;&#x9501;</span>

    <span class="hljs-keyword">private</span> Integer amount;

    <span class="hljs-function"><span class="hljs-keyword">public</span> Integer <span class="hljs-title">getAmount</span><span class="hljs-params">()</span> </span>{
        readLock.lock();
        <span class="hljs-keyword">try</span> {
            <span class="hljs-keyword">return</span> amount;
        } <span class="hljs-keyword">finally</span> {
            readLock.unlock();
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> Integer <span class="hljs-title">updateAmount</span><span class="hljs-params">(Integer a)</span> </span>{
        writeLock.lock();
        <span class="hljs-keyword">try</span> {
            <span class="hljs-keyword">this</span>.amount = a + (<span class="hljs-keyword">this</span>.amount == <span class="hljs-keyword">null</span> ? <span class="hljs-number">0</span> : <span class="hljs-keyword">this</span>.amount);
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.amount;
        } <span class="hljs-keyword">finally</span> {
            writeLock.unlock();
        }
    }
}

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ReentrantReadWriteLockTest</span> </span>{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> Account account = <span class="hljs-keyword">new</span> Account();

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> Random random = <span class="hljs-keyword">new</span> Random();

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        incr();
        load();
    }

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">incr</span><span class="hljs-params">()</span> </span>{

        <span class="hljs-keyword">new</span> Thread(() -&gt; {
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
                Integer leftAmount = <span class="hljs-number">0</span>;
                <span class="hljs-keyword">try</span> {
                    leftAmount = account.updateAmount(<span class="hljs-number">20</span>);
                    System.out.println(<span class="hljs-string">&quot;&#x5145;&#x503C;20&#x5143;&quot;</span>);
                    TimeUnit.MILLISECONDS.sleep(<span class="hljs-number">50</span>);
                } <span class="hljs-keyword">catch</span> (InterruptedException ex) {
                    ex.printStackTrace();
                } <span class="hljs-keyword">finally</span> {
                    System.out.println(<span class="hljs-string">&quot;&#x5145;&#x503C;&#x5B8C;&#x6210;:&quot;</span> + leftAmount);
                }
            }

        }).start();
    }

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">load</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">5</span>; i++) {
            <span class="hljs-keyword">new</span> Thread(() -&gt; {
                <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j &lt; <span class="hljs-number">5</span>; j++) {
                    System.out.println(<span class="hljs-string">&quot;&#x67E5;&#x4F59;&#x989D;&quot;</span> + account.getAmount());
                    threadSleep(<span class="hljs-number">100</span>);
                }
            }).start();
        }
    }
}
</code></pre>
<h1 id="threadpoolexecutorworker">ThreadPoolExecutor.Worker</h1>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; tgor.top 2019 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2020-04-23 15:32:53
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Java并发.html#synchronized" class="navigation navigation-prev " aria-label="Previous page: Java并发之synchronized">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Java之Lock.html#reentrantreadwritelock" class="navigation navigation-next " aria-label="Next page: Java Lock之ReentrantReadWriteLock">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Java Lock之ReentrantLock","level":"1.4.4","depth":2,"next":{"title":"Java Lock之ReentrantReadWriteLock","level":"1.4.5","depth":2,"anchor":"#reentrantreadwritelock","path":"Java-Base/Java之Lock.md","ref":"Java-Base/Java之Lock.md#reentrantreadwritelock","articles":[]},"previous":{"title":"Java并发之synchronized","level":"1.4.3","depth":2,"anchor":"#synchronized","path":"Java-Base/Java并发.md","ref":"Java-Base/Java并发.md#synchronized","articles":[]},"dir":"ltr"},"config":{"plugins":["chapter-fold","splitter","tbfed-pagefooter","page-toc-button","-search","search-pro","popup","back-to-top-button","code","copy-code-button"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy tgor.top 2019","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"splitter":{},"search-pro":{"cutWordLib":"nodejieba","defineWord":["Gitbook Use"]},"popup":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"code":{"copyButtons":true},"fontsettings":{"theme":"sepia","family":"serif","size":1},"highlight":{},"page-toc-button":{"maxTocDepth":2,"minTocSize":2},"back-to-top-button":{},"copy-code-button":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"tgor","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"tgor note","gitbook":"*","description":"tgor生活工作记录"},"file":{"path":"Java-Base/Java之Lock.md","mtime":"2020-04-23T07:32:53.268Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-04-29T08:43:24.127Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-page-toc-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

